﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using UIDATALayer;
namespace UserData
{
    public partial class Control_Tray : UserControl,Interface_UserControl
    {
        string strName = "默认料盘名称";
        [Category("料仓名称")]
        public string 料仓名称
        {
            set
            {
                strName = value;
            }
            get { return strName; }
        }



        TrayData trayData = null;
        private UIDATALayer.UIDataAction<TrayReflushData> acTrayReflushData = null;
        public Control_Tray()
        {
            InitializeComponent();

        }
        public void Init()
        {
            if (TrayDataMgr.GetInstance().dicTrayData.ContainsKey(strName))
            {
                this.trayData = TrayDataMgr.GetInstance().dicTrayData[strName];
                tool_Name.Text = strName;
                this.Text = strName;
                trayData.OnTestTrayDataChange += TrayData_OnTestTrayDataChange;
                TrayData_OnTestLoadTrayData(trayData.RowCount, trayData.ColCount);
            }

        }

        private void TrayData_OnTestTrayDataChange()
        {
            ReflushUI();
        }

        private void TrayData_OnTestLoadTrayData(int n_Row, int n_Column)
        {
            table_Tray.RowStyles.Clear();
            table_Tray.ColumnStyles.Clear();
            table_Tray.Controls.Clear();
            table_Tray.RowCount = (int)n_Row;
            table_Tray.ColumnCount = (int)n_Column;
            for (int i = 0; i < n_Row; i++)
            {
                table_Tray.RowStyles.Add(new RowStyle(SizeType.Percent, (float)1.0 / n_Row));
                for (int j = 0; j < n_Column; j++)
                {
                    table_Tray.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, (float)1.0 / n_Column));
                    UIPanel panel = new UIPanel();
                    panel.Dock = DockStyle.Fill;
                    panel.FillColor = Color.Gray;
                    panel.Text = (j + i * n_Column + 1).ToString();
                    panel.Tag= (j + i * n_Column + 1).ToString();
                    table_Tray.Controls.Add(panel, j, i);
                    panel.DoubleClick += Panel_DoubleClick;
                }
            }
            ReflushUI();
        }
        private void Panel_DoubleClick(object sender, EventArgs e)
        {
            UIPanel btn = (UIPanel)sender;
            int nRow = (int.Parse(btn.Tag.ToString()) - 1) / trayData.ColCount;
            int nColumn = (int.Parse(btn.Tag.ToString()) - 1) % trayData.ColCount;
            trayData.SetRowAndColumn(nRow, nColumn);
        }

        private void ReflushUI()
        {
            if(acTrayReflushData==null)
            {
                UIDATALayerHelper.RegistAction<TrayReflushData>(nameof(TrayReflushData), ReflushUI).Start();
                acTrayReflushData = UIDATALayer.UIDATALayerHelper.GetRegistAction<TrayReflushData>(nameof(TrayReflushData));
                acTrayReflushData.SetQueueNum(100);
            }
            TrayReflushData data = new TrayReflushData();
            ReflushUI(data);
        }
        private void ReflushUI(TrayReflushData data)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new Action(() => ReflushUI(data)));
            }
            else
            {
                if (table_Tray.Controls.Count == 0)
                    return;
                int n_NowIndex = trayData.RowNow * trayData.ColCount + trayData.ColNow;
                for (int i = 0; i < table_Tray.Controls.Count; i++)
                {
                    if(!trayData.list_TrayCellResult[i])
                        ((UIPanel)table_Tray.Controls[i]).FillColor = Color.Red;
                    else if(i< n_NowIndex)
                        ((UIPanel)table_Tray.Controls[i]).FillColor = Color.Green;
                    else if (i == n_NowIndex)
                        ((UIPanel)table_Tray.Controls[i]).FillColor = Color.Yellow;
                    else
                        ((UIPanel)table_Tray.Controls[i]).FillColor = Color.Gray;
                    ((UIPanel)table_Tray.Controls[i]).Text = trayData.list_TrayCellName[i];
                }
            }
        }
        private void tool_Reset_Click(object sender, EventArgs e)
        {
            if (UIMessageBox.ShowAsk($"是否重置料盘:{ tool_Name.Text }的行数列数?"))
            {
                trayData.SetRowAndColumn(0, 0);
            }

        }

        private void Control_Tray_Load(object sender, EventArgs e)
        {

        }
    }
}
